package com.zenghm.recyclerview.expandable;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zenghm.entity.Person;
import com.zenghm.entity.Student;
import com.zenghm.entity.Teacher;
import com.zenghm.recyclerview.databinding.ItemPersonBinding;

import java.util.ArrayList;
import java.util.List;

public class ExpandableRecyclerViewAdapter extends BaseExpandableAdapter<ExpandableRecyclerViewAdapter.VH> {

    private final List<Teacher> teacherList;

    public ExpandableRecyclerViewAdapter() {
        List<Teacher> teacherList = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {

            int rand = i % 5 + 1;
            List<Student> studentList = new ArrayList<>();
            for (int j = 0; j < rand; j++) {
                Student student = new Student();
                student.setName("Student" + (i * 10 + j));
                student.setAge(16);
                studentList.add(student);
            }

            Teacher teacher = new Teacher();
            teacher.setName("Teacher" + i);
            teacher.setAge(30);
            teacher.setStudentList(studentList);
            teacherList.add(teacher);
        }
        this.teacherList = teacherList;
    }

    private <T> int size(List<T> list) {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getGroupCount() {
        return size(teacherList);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupPosition < getGroupCount()) {
            return size(teacherList.get(groupPosition).getStudentList());
        }
        return 0;
    }

    @Override
    public int getGroupViewType(int groupPosition) {
        return 0;
    }

    @Override
    public int getChildViewType(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public void onBindGroupViewHolder(@NonNull VH holder, final int groupPosition) {
        Teacher teacher = teacherList.get(groupPosition);
        holder.setData(teacher);
        holder.setOnClickListener(v -> toggle(groupPosition));
    }

    @Override
    public void onBindChildViewHolder(@NonNull VH holder, int groupPosition, int childPosition) {
        Student student = teacherList.get(groupPosition).getStudentList().get(childPosition);
        holder.setData(student);
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        final LayoutInflater inflater = LayoutInflater.from(context);
        ItemPersonBinding binding = ItemPersonBinding.inflate(inflater, parent, false);
        return new ExpandableRecyclerViewAdapter.VH(binding);
    }

    static class VH extends RecyclerView.ViewHolder {

        private final ItemPersonBinding mBinding;

        private VH(ItemPersonBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void setData(Person person) {
            mBinding.tvName.setText(person.getName());
            if (person instanceof Teacher) {
                itemView.setBackgroundColor(0XFF7FCFF4);
            } else {
                itemView.setBackgroundColor(0XFFEFCFF4);
            }
        }

        public void setOnClickListener(View.OnClickListener listener) {
            itemView.setOnClickListener(listener);
        }
    }
}